<template>
  <el-dialog title="分配角色" :visible="showRole" @close="closeEvent">

    <h3>{{ checked }}</h3>

    <el-checkbox v-for="item in list" :key="item.id" v-model="checked" :label="item.id">{{ item.name }}</el-checkbox>

    <template #footer>
      <div style="text-align: center;">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeEvent">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysRole } from '@/api/setting'
import { sysUser } from '@/api/user'
import { sysUserAssignRolesPut } from '@/api/employees'
export default {
  props: {
    showRole: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      show: false,
      checked: [],
      list: [],
      id: ''
    }
  },
  methods: {
    closeEvent() {
      this.$emit('update:showRole', false)
      this.checked = []
    },
    // 获取角色列表
    async  getData() {
      if (this.list.length === 0) {
        const res = await sysRole({
          page: 1,
          pagesize: 999999
        })
        this.list = res.data.rows
        console.log('角色列表', res)
      }
    },
    // 获取角色详情
    async  getRoleInfo(id) {
      this.id = id
      const res = await sysUser(id)
      this.checked = res.data.roleIds || [] // 解决后端返回值为null问题
      console.log('角色详情', res)
    },
    // 确定调用
    async submit() {
      await sysUserAssignRolesPut({
        id: this.id,
        roleIds: this.checked
      })
      this.$message.success('分配角色成功')
      this.closeEvent()
    }
  }
}
</script>
<style>
</style>
